package net.alche.ext.dbUtil.connect.dbs;
import net.alche.ext.dbUtil.connect.DBConnect;
import net.alche.ext.dbUtil.connect.def.DbConfig;
import java.sql.Types;

//
// 数据库操作相关工具类
//
// @author:姚礼武
// @time: [2018年04月11 13:43:54 ]
// @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
//
public class DBUtil {

  public static DBConnect getDbConnect(DbConfig config) {
    DBConnect dbConnect = null;
    if (config.type == Config.MYSQL) {
      dbConnect = new DBConnectMysql(config);
    }
    else if ( config.type == Config.MSSQL) {
      dbConnect = new DBConnectMsSql(config);
    }
    else if ( config.type == Config.ORACLE) {
      dbConnect = new DBConnectOracle(config);
    }
    return dbConnect;
  }

  //
  // 通过表名称转换成类名称
  //
  // @param tableName 表名称
  // @return
  //
  public static String getPojoNameByTableName(String tableName) {
    StringBuilder sb = new StringBuilder();
    String[] strs = tableName.split("_");
    for (String str: strs) {
      if (str.equals("T")) {
        continue;
      }
      sb.append(str.substring(0, 1).toUpperCase()).append(str.substring(1).toLowerCase());
    }
    return sb.toString();
  }

  //
  // 通过表名称转换成类变量声明(首字母小写)
  //
  // @param tableName 表名称
  // @return
  //
  public static String getPojoVarNameByTableName(String tableName) {
    StringBuilder sb = new StringBuilder();
    String[] strs = tableName.split("_");
    boolean first = true;
    for (String str: strs) {
      if (str.equals("T")) {
        continue;
      }
      if (first) {
        first = false;
        sb.append(str.toLowerCase());
      }
      else {
        sb.append(str.substring(0, 1).toUpperCase()).append(str.substring(1).toLowerCase());
      }
    }
    return sb.toString();
  }

  //
  // 获取表的别名
  //
  // @param tableName 表名称
  // @return
  //
  public static String getTableAlias(String tableName) {
    String[] strs = tableName.split("_");
    for (String str: strs) {
      if (str.length() <= 1 || str.equals("T") || str.equals("ZTEST") || str.equals("TEST")) {
        continue;
      }
      if (str.length() <= 3 && tableName.length() != str.length()) {
        return str.toUpperCase();
      }
      else {
        return str.substring(0, 1).toUpperCase();
      }
    }
    return null;
  }

  //
  // 获取jdbcType类型名称
  //
  // @param dataType 字段数据类型(对应java.pdm.Types中的常量)
  // @return
  //
  public static String getJdbcType(Integer type) {
    switch (type) {
      case Types.BIT : {
        return "BIT";
      }
      case Types.TINYINT : {
        return "TINYINT";
      }
      case Types.SMALLINT : {
        return "SMALLINT";
      }
      case Types.INTEGER : {
        return "INTEGER";
      }
      case Types.BIGINT : {
        return "BIGINT";
      }
      case Types.FLOAT : {
        return "FLOAT";
      }
      case Types.REAL : {
        return "REAL";
      }
      case Types.DOUBLE : {
        return "DOUBLE";
      }
      case Types.NUMERIC : {
        return "NUMERIC";
      }
      case Types.DECIMAL : {
        return "DECIMAL";
      }
      case Types.CHAR : {
        return "CHAR";
      }
      case Types.VARCHAR : {
        return "VARCHAR";
      }
      case Types.LONGVARCHAR : {
        return "LONGVARCHAR";
      }
      case Types.DATE : {
        return "DATE";
      }
      case Types.TIME : {
        return "TIME";
      }
      case Types.TIMESTAMP : {
        return "TIMESTAMP";
      }
      case Types.BINARY : {
        return "BINARY";
      }
      case Types.VARBINARY : {
        return "VARBINARY";
      }
      case Types.LONGVARBINARY : {
        return "LONGVARBINARY";
      }
      case Types.NULL : {
        return "NULL";
      }
      case Types.OTHER : {
        return "OTHER";
      }
      case Types.JAVA_OBJECT : {
        return "JAVA_OBJECT";
      }
      case Types.DISTINCT : {
        return "DISTINCT";
      }
      case Types.STRUCT : {
        return "STRUCT";
      }
      case Types.ARRAY : {
        return "ARRAY";
      }
      case Types.BLOB : {
        return "BLOB";
      }
      case Types.CLOB : {
        return "CLOB";
      }
      case Types.REF : {
        return "REF";
      }
      case Types.DATALINK : {
        return "DATALINK";
      }
      case Types.BOOLEAN : {
        return "BOOLEAN";
      }
      case Types.ROWID : {
        return "ROWID";
      }
      case Types.NCHAR : {
        return "NCHAR";
      }
      case Types.NVARCHAR : {
        return "NVARCHAR";
      }
      case Types.LONGNVARCHAR : {
        return "LONGNVARCHAR";
      }
      case Types.NCLOB : {
        return "NCLOB";
      }
      case Types.SQLXML : {
        return "SQLXML";
      }
      case Types.REF_CURSOR : {
        return "REF_CURSOR";
      }
      case Types.TIME_WITH_TIMEZONE : {
        return "TIME_WITH_TIMEZONE";
      }
      case Types.TIMESTAMP_WITH_TIMEZONE : {
        return "TIMESTAMP_WITH_TIMEZONE";
      }
      default : {
        return null;
      }
    }
  }
}